#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <list>
#include <SFML/Audio.hpp>
using namespace std;

#if _DEBUG
	#pragma comment( lib, "sfml-system-s-d.lib" )
	#pragma comment( lib, "sfml-graphics-s-d.lib" )
	#pragma comment( lib, "sfml-audio-s-d.lib" )
	#pragma comment( lib, "sfml-Window-s-d.lib" )
#else
	#pragma comment( lib, "sfml-system-s.lib" )
#endif

class Missile
{
public:
	void update( float dt );
	void render();

	Missile( float x, float y);

	float GetX( void ) { return sprite.GetPosition().x; }
private:
	sf::Image image;
	sf::Sprite sprite;
};

sf::RenderWindow App(sf::VideoMode(800,600), "SFML Window" );


 int main()
 {
	 list< Missile *> missiles;
	 sf::Clock clock;

	 sf::Image image;

	 if (!image.LoadFromFile("../cute_image.jpg"))
	 {
		 return -1;
	 }

	 sf::Shape polygon;
	 polygon.AddPoint (0,0, sf::Color(255,255,0), sf::Color(0,0,128));
     polygon.AddPoint (50,0, sf::Color(255,255,0), sf::Color(0,0,128));
	 polygon.AddPoint (50,50, sf::Color(255,255,0), sf::Color(0,0,128));
	 polygon.AddPoint (0,50, sf::Color(255,255,0), sf::Color(0,0,128));

	 polygon.SetOutlineWidth( 10 );

	 sf::SoundBuffer soundBuffer;
	 soundBuffer.LoadFromFile("smooth_sting.wav");
	 sf::Sound sound;

	 sound.SetBuffer(soundBuffer);

	 polygon = sf::Shape::Circle( 0, 0, 50, sf::Color( 0, 255, 0), 5, sf::Color( 0,0,255) );

	 sf::Vector2f point = polygon.GetPointPosition(0 );

	 polygon.SetPosition( 100, 100 );


	 sf::String string;
	 string.SetFont(sf::Font::GetDefaultFont());
	 string.SetText("eiurht");

	 sf::Music music;
	 music.OpenFromFile("Dora_MiniGame1.ogg");

	 music.Play();



	 sf::Sprite sprite( image);
	 sprite.SetPosition(400.0f,300.0f);
	 //sprite.Resize(50,50);
	 sprite.SetScale(1.0f,1.0f);
	 const sf::Input& Input = App.GetInput();
	 bool Running = true;
	 while (App.IsOpened())
	 {

		 sf::Event Event;
		 while (App.GetEvent(Event))
		 {
			 if (Event.Type == sf::Event::Closed)
				 App.Close();

			 if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
				 Running = false;

			 if ( Event.Type == sf::Event::MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Left )
			 {
				 missiles.push_back( new Missile( App.GetInput().GetMouseX(), App.GetInput().GetMouseY()) );
				 sound.Play();
			 }

			 
		 }

		 sprite.SetPosition(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());


		 if (App.GetInput().IsKeyDown(sf::Key::Left))	sprite.Move( -10005 * clock.GetElapsedTime(), 0 );
		 if (App.GetInput().IsKeyDown(sf::Key::Right))  sprite.Move( 10005 * clock.GetElapsedTime() ,0 );
		 if (App.GetInput().IsKeyDown(sf::Key::Up))		sprite.Move( 0, -10005 * clock.GetElapsedTime() );
		 if (App.GetInput().IsKeyDown(sf::Key::Down))   sprite.Move( 0, 10005 * clock.GetElapsedTime() );
	     

		         // Clear screen
        App.Clear(sf::Color(200, 0, 0));
		//sprite.SetRotation(-clock.GetElapsedTime() * 50);
		//sprite.Rotate(45);

		// update and render missiles
		 list<Missile*>::iterator iter = missiles.begin();
		 while ( iter != missiles.end() )
		 {
			 (*iter)->update(clock.GetElapsedTime());

			 if ( (*iter)->GetX() > 700.0f )
			 {
				 delete *iter;
				 iter = missiles.erase( iter );
				 continue;
			 }
			 else
			 {
				 (*iter)->render();
			 }
			 
			 ++iter;
		 }
		 App.Draw(string);
          App.Draw(polygon);
		//App.Draw(sprite);
		 App.Display();
		 clock.Reset();
	 }



	 return EXIT_SUCCESS;
	
 }

 Missile::Missile(float x, float y)
 {
	 image.LoadFromFile("Missile.jpg");
	 sprite.SetImage(image);
	 sprite.SetPosition( x, y );
 }

 void Missile::update( float dt )
 {
	 sprite.Move( 1000 * dt, 0 );
 }

 void Missile::render()
 {
	 App.Draw( sprite );
 }